package com.qqt.csr.web.dto.member;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.qqt.csr.common.acl.aics.resp.ChannelInfoDTO;
import com.qqt.csr.im.entity.Account;
import lombok.*;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;

import java.io.Serial;
import java.io.Serializable;
import java.util.Optional;

@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
@JsonIgnoreProperties(ignoreUnknown = true)
public class VisitorInfoDTO implements Serializable {
    @Serial
    private static final long serialVersionUID = -9080589737272942828L;

    /**
     * 租户id
     */
    private String tenantId;

    /**
     * 访客id
     */
    private String userId;
    /**
     * 访客名称
     */
    private String userName;
    /**
     * 访客头像
     */
    private String headImg;

    /**
     * 渠道id
     */
    private String channelId;
    /**
     * 渠道名称
     */
    private String channelName;

    /**
     * IM账号信息
     */
    private Account account;

    public static VisitorInfoDTO of(String tenantId, String userId, ChannelInfoDTO channelInfo) {
        return VisitorInfoDTO.builder()
                .tenantId(tenantId)
                .userId(userId)
                .channelId(channelInfo.getId())
                .channelName(channelInfo.getName())
                .build();
    }

    public void bindImAccount(Account account) {
        this.account = account;
        this.headImg = Optional.ofNullable(account).map(Account::getHeadImg).orElse(StringUtils.EMPTY);
        this.userName = Optional.ofNullable(account).map(Account::getAccountName).orElse(StringUtils.EMPTY);
    }
}
